package com.house365.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import android.util.Log;

public class TimeUtil {

	public static long getCurrentTime(){
		long timedeta = System.currentTimeMillis() / 1000;
		return timedeta;
	}
	
	public static String getBetween(long time) {
		long timedeta = System.currentTimeMillis() / 1000 - time;
		if (timedeta < 3600) {
			return Math.round(timedeta / 60) + "分钟";
		} else if (timedeta < (3600 * 24)) {
			return Math.round(timedeta / 3600) + "小时";
		} else {
			int n = Math.round(timedeta / (3600 * 24));
			if (n < 3) {
				return n + "天";
			} else {
				return toDate(time);
			}
		}
	}

	/**
	 * 同getBetween，避免显示"-1分钟"
	 */
    public static String getBetweenFixed(long time) {
        long timedeta = System.currentTimeMillis() / 1000 - time;
        if (timedeta < 3600) {
            int v = Math.round(timedeta / 60);
            if (v < 0) {
                v = 0;
            }
            return v + "分钟";
        } else if (timedeta < (3600 * 24)) {
            return Math.round(timedeta / 3600) + "小时";
        } else {
            int n = Math.round(timedeta / (3600 * 24));
            if (n < 3) {
                return n + "天";
            } else {
                return toDate(time);
            }
        }
    }

	public static String toDayAndHour(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		return sdf.format(new Date(time*1000)).toString();
	}
	public static String toDateAndTime(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(time*1000)).toString();
	}
	public static String toTime(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		return sdf.format(new Date(time*1000)).toString();
	}
	public static String toDate(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date(time*1000)).toString();
	}
	
	public static String toDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date()).toString();
	}
	
	public static String toYear(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return sdf.format(new Date(time*1000)).toString();
	}
	public static String toMonth(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		return sdf.format(new Date(time*1000)).toString();
	}
	public static String toDay(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		return sdf.format(new Date(time*1000)).toString();
	}

	public static String toNDate(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(time * 1000).toString();
	}

	public static String toMDate(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy��MM��dd��");
		return sdf.format(new Date(time*1000)).toString();
	}
	public static String toWeekAndDate(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy��MM��dd�� HH:mm:ss");
		return sdf.format(new Date(time*1000)).toString();
	}

	public static String toDateWithFormat(long time, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(time*1000)).toString();
	}

//	public static int getBetweenDays(long start, long end) {
//		return ((int) ((end - start) / (long) (60 * 60 * 24))) + 1;
//	}

	public static String formatLongToTimeStr(Long l, String format) {
		int day = 0;
		int hour = 0;
		int minute = 0;
		int second = 0;

		second = (int) Math.floor(l / 1000);
		if (second > 60) {
			minute = second / 60;
			second = second % 60;
		}
		if (minute > 60) {
			hour = minute / 60;
			minute = minute % 60;
		}
		if (hour > 24) {
			day = hour / 24;
			hour = hour % 24;
		}
		String str = StringUtils.replaceEach(format, new String[] { "dd", "HH",
				"mm", "ss" }, new String[] { String.format("%0$02d", day),
				String.format("%0$02d", hour), String.format("%0$02d", minute),
				String.format("%0$02d", second) });
		return str;
	}
	
	//��ǰʱ����Ƿ���
	public static boolean isCurday(long time,String date){
		String curDay =toDate(time);
		if(curDay.equals(date)){
			return true;
		}else{
			return false;
		}
	}
	/***
	 * 获取两个给定时间戳的时间间隔.
	 * 
	 * @deprecated 该方法浪费内存,且可能导致CPU内存耗尽,请勿使用.
	 * @param startime
	 * @param endtime
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getBetweenCalendar(long startime, long endtime) {
		List<String> list = new ArrayList<String>();
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
		Date dateBegin = null;
		Date dateEnd = null;
		try {
			dateBegin = formater.parse(formater.format((startime-86400)*1000));
			dateEnd = formater.parse(formater.format((endtime-86400)*1000));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		if (dateBegin != null && dateEnd != null) {
			while (dateBegin.compareTo(dateEnd) <= 0) {
				calendar.setTime(dateBegin);
				calendar.add(calendar.DATE, 1);
				dateBegin = calendar.getTime();
				list.add(formater.format(dateBegin));
			}
		}
		return list;
	}
	public static String toDateStrWithFormat(Date date,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date).toString();
	}
	public static Date parseDate(String s,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(s);
		} catch (Exception e) {
			return null;
		}
	}
	public static long getDateSeconds(String s,String format){
		Date d = parseDate(s, format);
		if(d!= null){
			return d.getTime()/1000;
		}
		return System.currentTimeMillis()/1000;
	
	}
}
